home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / paul_qureshi / info / spline2.txt < prev    next >
Text File  |  1999-03-27  |  1KB  |  67 lines

  1.  
  2. Below is code for a simple fixed point recursive de Casteljau
  3. algorithm. Ive been racking my brains over this one,
  4. but I cant figure out the equivalent array based algo.
  5.  
  6. -> assumes 16 bit shorts, 32 bit longs
  7.  
  8. // this one used internally only
  9. void _spline(long *_x, long *_y, byte color, int depth);
  10.  
  11. // this one called from main, size of x, y should be 4
  12. void spline(short *x, short *y, byte color, int depth)
  13. {
  14.     long _x[4], _y[4];
  15.  
  16.     for(int i=0;i<4;i++)
  17.     {
  18.         _x[i] =    long(x[i]) << 16;
  19.         _y[i] = long(y[i]) << 16;
  20.     }
  21.     _spline(_x,_y,color,depth);
  22. }
  23.  
  24.  
  25. void _spline(long *_x, long *_y, byte color, int depth)
  26. {
  27.     if(depth==0)
  28.     {
  29.         short x[4], y[4];
  30.         for(int i=0;i<4;i++)
  31.         {
  32.             x[i] = _x[i] >> 16;
  33.             y[i] = _y[i] >> 16;
  34.         }
  35.         polyline(x,y,4,color);
  36.     }
  37.     else
  38.     {
  39.         long hx,hy, px[4], py[4], rx[4], ry[4];
  40.  
  41.         px[0] = _x[0]; py[0] = _y[0];
  42.         rx[3] = _x[3]; ry[3] = _y[3];
  43.  
  44.         px[1] = (_x[0]+_x[1]) / 2;
  45.         py[1] = (_y[0]+_y[1]) / 2;
  46.  
  47.         rx[2] = (_x[2]+_x[3]) / 2;
  48.         ry[2] = (_y[2]+_y[3]) / 2;
  49.  
  50.         hx        = (_x[1]+_x[2]) / 2;
  51.         hy        = (_y[1]+_y[2]) / 2;
  52.  
  53.         px[2] = (px[1]+hx) / 2;
  54.         py[2] = (py[1]+hy) / 2;
  55.  
  56.         rx[1] = (rx[2]+hx) / 2;
  57.         ry[1] = (ry[2]+hy) / 2;
  58.  
  59.         rx[0] = px[3] = (px[2]+rx[1]) / 2;
  60.         ry[0] = py[3] = (py[2]+ry[1]) / 2;
  61.  
  62.         _spline(px, py, color, depth-1);
  63.         _spline(rx, ry, color, depth-1);
  64.     }
  65. }
  66.  
  67.